#!/usr/bin/env sh
# ^^^^^^^^^^^^^^^ Husky doesn't require this shebang,
# but code editors need it to recognize the file as a script.

# Format and add the changes to the staging area
npm run format
git add --update

# POSIX shell (sh) run time parameters.
#   -e: Exit immediately if a command exits with a non-zero status.
#   -u: Treat unset variables as errors when substituting.
set -eu

# Track exit code in case that we want to add other lints in the future
EXITCODE=0

# If there are changed icons, lint them with SVGLint
changed_icons="$(git diff --cached --name-only | grep '^icons/' | xargs)"
if [ -n "$changed_icons" ]; then
	# Extract svglint command (without file arguments) from package.json using Node.js
	svglint_script_no_icons="$(node -p "const s=require('./package.json').scripts.svglint.split(' '); s.pop(); s.join(' ')")"
	echo "+ npx $svglint_script_no_icons $changed_icons"
	npx $svglint_script_no_icons $changed_icons || EXITCODE=$?
fi

exit $EXITCODE
